package com.jlkf.controller;



import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jlkf.service.UserService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.StringTools;


@Controller
@RequestMapping("user")
public class UserController {
	private static final Logger log=Logger.getLogger(UserController.class);
	
	@Autowired
	private UserService userService;
	
	/**
	 *  账号登录
	 * @param json
	 * @return
	 */
	 @RequestMapping(value="loginUser")
	 @ResponseBody
	 public ReturnData  loginUser(@RequestBody String json,HttpServletRequest request){
		log.info("测试=============="+request.getServerPort()); 
		return userService.loginUser(json);
	 }
	
	
	 /**
	 * 发送短信验证码
	 * @param request
	 * @param phone
	 * @param type 1注册 2忘记密码 3验证码登录 4绑定手机号
	 * @return
	 */
	@RequestMapping("sendCode")
	@ResponseBody
	public Object sendCode(HttpServletRequest request,String phone,Integer type) {
		
		try {
			if(StringTools.isEmpty(phone)) {
				return new ReturnData(0, "手机号不能为空");
			}else if (type == null) {
				return new ReturnData(1, "类型不能为空");
			}
			
			return userService.sendCode(phone,type);
		} catch (Exception e) {
			return new ReturnData(0, "传入参数有误");
		}

	}
	 
	 /**
	  * 用户注册
	  * @param json
	  * @return
	  */
	 @ResponseBody
	 @RequestMapping(value="regisUser")
	 public ReturnData regisUser(@RequestBody String json){
		 
		return userService.regisUser(json);
	 }
	 /**
	  *  手机验证码登录
	  * @param phone
	  * @param code
	  * @return
	  */
	 @ResponseBody
	 @RequestMapping(value="codeLogin")
	 public ReturnData codeLogin(String phone,String code){
		 
		return userService.codeLogin(phone,code);
	 }
	 
	 /**
	  * 忘记密码
	  * @param json
	  * @return
	  */
	@ResponseBody
	@RequestMapping(value="forgetPassWord")
	public ReturnData forgetPassWord(@RequestBody String  json){
		
		return userService.forgetPassWord(json);
	}
	
	
	/**
	 *   第三方隐形注册
	 * @param json
	 * @return
	 */
	@RequestMapping(value="authorizedLogin")
	@ResponseBody
	public ReturnData  authorizedLogin(@RequestBody String json){
		
		return userService.authorizedLogin(json);
	}
	/**
	 *  判断是否绑定过第三方登录 ，没有绑定需要绑定手机号，否则直接登录成功
	 * @param authorizedInfo
	 * @param type
	 * @return
	 */
   @RequestMapping(value="isBind")
   @ResponseBody
   public ReturnData  isBind(String authorizedInfo,Integer type){
	   
	return userService.isBind(authorizedInfo,type);
   }
   
   /**
    *   选择兴趣 (第一层分类)
    * @param pageSize
    * @param pageNumber
    * @return
    */
   @RequestMapping(value="levelOneSort")
   @ResponseBody
   public ReturnData levelOneSort(Integer pageSize, Integer pageNumber){
	   
	return userService.levelOneSort(pageSize,pageNumber);
   }
   
    /**
     *  修改用户消息
     * @param pageSize
     * @param pageNumber
     * @return
     */
    @RequestMapping(value="updateUserInfo")
    @ResponseBody
    public ReturnData updateUserInfo(@RequestBody String json){
 	   
    	return userService.updateUserInfo(json);
    }
    
    /**
     *  个人信息
     * @param userId
     * @return
     */
    @RequestMapping(value="userInfo")
    @ResponseBody
    public ReturnData userInfo(Long userId){
    	
		return userService.userInfo(userId);
    }
    
    /**
     * 我的余额
     */
   	@RequestMapping("getBalance")
   	@ResponseBody
   	public Object getBalance(Long userId) {
   		return userService.getBalance(userId);
   	}
   	
   	/**
   	 * 查看我的会员
   	 */
   	@RequestMapping("getVIP")
   	@ResponseBody
   	public Object getVIP(Long userId) {
   		return userService.getVIP(userId);
   	}
   	
   	/**
   	 * 钱包交易记录
   	 */
   	@RequestMapping("getBalanceRecord")
   	@ResponseBody
   	public Object getVIP(Long userId,Integer pageNumber,Integer pageSize) {
   		return userService.getBalanceList(userId, pageNumber, pageSize);
   	}
   	
   	/**
   	 * 分享链接
   	 */
   	@RequestMapping("enjoyLink")
   	@ResponseBody
   	public Object enjoyLink(Long userId) {
   		return userService.enjoyLink(userId);
   	}
   	
   	/**
   	 * 修改帐户密码
   	 */
   	@RequestMapping(value="updatePwd")
   	@ResponseBody
   	public Object updatePwd(@RequestBody String json) {
   		return userService.updatePwd(json);
   	}
   	
   	/**
   	 * 设置支付密码
   	 */
   	@RequestMapping(value="setPayPwd")
   	@ResponseBody
   	public Object setPayPwd(@RequestBody String json) {
   		return userService.setPayPwd(json);
   	}
   	
   	/**
   	 * 修改支付密码
   	 */
   	@RequestMapping(value="updatePayPwd")
   	@ResponseBody
   	public Object updatePayPwd(@RequestBody String json) {
   		return userService.updatePayPwd(json);
   	}
   	
   	/**
	  * 忘记支付密码
	  * @param json
	  * @return
	  */
	@ResponseBody
	@RequestMapping(value="forgetPayPwd")
	public ReturnData forgetPayPwd(@RequestBody String  json) throws NoSuchAlgorithmException, InvalidKeySpecException {	
		return userService.forgetPayPwd(json);
	}
	
	/**
	  * 修改绑定手机号
	  * @param json
	  * @return
	  */
	@ResponseBody
	@RequestMapping(value="updatePhone")
	public ReturnData updatePhone(@RequestBody String  json){	
		return userService.updatePhone(json);
	}
	
	/**
	 * 是否设置了支付密码
	 */
	@ResponseBody
	@RequestMapping(value="isSetPayPwd")
	public ReturnData isSetPayPwd(Long userId){	
		return userService.isSetPayPwd(userId);
	}
	
	/**
	 * 根据手机号绑定第三方
	 */
	@ResponseBody
	@RequestMapping(value="bandThird")
	public ReturnData bandThird(@RequestBody String json){	
		return userService.bandThird(json);
	}
	
	/**
	 * 微信登录获取用户信息
	 */
	@ResponseBody
	@RequestMapping(value="getWeixinInfo")
	public ReturnData getWeixinInfo(String code,Integer type){	
		return userService.getWeixinInfo(code,type);
	}
	
	/**
	 * 更新用户mac地址
	 */
	@ResponseBody
	@RequestMapping(value="/updateMac",method = RequestMethod.POST)
	public ReturnData updateMac(Integer userid,String mac){	
		return userService.updateMac(userid,mac);
	}
}
